import { Global, MiddlewareConsumer, Module, NestModule } from "@nestjs/common";
import { ClsModule } from "nestjs-cls";

import { AuthModule } from "./auth/auth.module";
import { CoreConfigModule } from "./config/core-config.module";
import { DbModule } from "./db/db.module";
import { RequestContextMiddleware } from "./logger/request-context.middleware";
import { RequestLogMiddleware } from "./logger/request-log.middleware";
import { TasksModule } from "./tasks/tasks.module";

@Global()
@Module({
    imports: [
        ClsModule.forRoot({
            global: true,
            middleware: {
                mount: true
            }
        }),
        AuthModule,
        CoreConfigModule,
        DbModule,
        TasksModule
    ]
})
export class CoreModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        // TODO: 怎么设置中间件只应用于 /api/ 开头的路径？目前 /api/* 无效，似乎把它当成一个字符串路径了
        consumer.apply(RequestContextMiddleware, RequestLogMiddleware).forRoutes("*");
    }
}
